﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_WhatCookToDay_Search
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadDataSearch(1)
        End If
    End Sub

#Region "Sub and function"
    Sub LoadDataSearch(Optional ByVal iPage As Integer = 1)
        Dim arrSearch() As String
        arrSearch = Session("search")
        If (Not arrSearch Is Nothing) Then
            Dim iPageSize As Integer = hidPageSize.Value
            Using ssmdata As New VSEntities
                Dim objDateOnTv As Object = Nothing
                'If (arrSearch(3).ToString <> "Ngày phát sóng") Then
                '    objDateOnTv = CType(arrSearch(3), Date)
                'End If
                Dim kq = ssmdata.uspSearchWhatCookToday(IIf(arrSearch(0).ToString = "Tên món ăn", Nothing, arrSearch(0).ToString), arrSearch(1), arrSearch(2), objDateOnTv).ToList()
                Dim r = kq.Skip((iPage - 1) * iPageSize).Take(iPageSize).ToList
                hidCountOther.Value = kq.Count
                'lblCount.Text = "Có " & kq.Count & " bài viết phù hợp với tìm kiếm"
                If (kq.Count > 0) Then
                    Create_PagerOther(kq.Count, iPage, iPageSize, 10)
                End If

                With rptContentByCat
                    .DataSource = r
                    .DataBind()
                End With
            End Using
        End If
    End Sub
    
#End Region
    Protected Sub rptContentByCat_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptContentByCat.ItemDataBound
        Dim p = CType(e.Item.DataItem, Object)

        Dim lblimg As Label = CType(e.Item.FindControl("lblimg"), Label)
        If ((e.Item.ItemIndex + 1) Mod 4 = 0 AndAlso e.Item.ItemIndex <> 0) Then
            lblimg.Text = "<img src=""" & MainDomain & "images/img_space.jpg"" Style=""margin: 15px 0 5px -7px; !Important"" class = ""beautyLineGray"" />"
        Else
            lblimg.Text = ""
        End If

        Dim imgNews As Image = CType(e.Item.FindControl("imgNews"), Image)
        If (Not p.Image Is Nothing) Then
            imgNews.ImageUrl = p.Image.ToString.Replace("~/Uploads", "")
        End If
        imgNews.ImageAlign = ImageAlign.Middle

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = p.Title
        hplTitle.NavigateUrl = "~/" & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & p.QueryStringFT.ToString & "/" & p.QueryString.ToString & "-" & p.Id.ToString & ".html"

        Dim lblDesc As Label = CType(e.Item.FindControl("lblDesc"), Label)
        lblDesc.Text = p.Description.ToString
        
    End Sub

#Region "Cac thu tuc phan trang"
    Sub Create_PagerOther(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndexOther_page.Value = TotalPage
        'gan lai curPage de set active
        hidCurOther_Page.Value = Page_Index
        'generate ra left page
        If TotalPage > 1 And Page_Index <> 1 Then
            lnkFirstOther.Enabled = True
        Else
            lnkFirstOther.Enabled = False
        End If

        If TotalPage > 1 And Page_Index <> TotalPage Then
            lnkLastOther.Enabled = True
        Else
            lnkLastOther.Enabled = False
        End If
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPageOther
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPageOther_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPageOther.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = CType(e.Item.FindControl("lnkTitleOther"), LinkButton)
        'lnkTitle.CausesValidation = False
        'dang ky async ko postback
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCurOther_Page.Value Then
            lnkTitle.CssClass = "activePageFaq"
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        Else
            ' lnkTitle.CssClass = "pagesLink"
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitleOther_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        'ham bind data - take skip data
        LoadDataSearch(CInt(lnkTile.ToolTip.ToString))
        
    End Sub
    Protected Sub lnkFirstOther_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirstOther.Click

        hidCurOther_Page.Value = hidCurOther_Page.Value - 1

        LoadDataSearch(CInt(hidCurOther_Page.Value))
    End Sub
    Protected Sub lnkLastOther_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLastOther.Click
        hidCurOther_Page.Value = hidCurOther_Page.Value + 1
        LoadDataSearch(CInt(hidCurOther_Page.Value))
    End Sub
#End Region
End Class
